package com.java.jetpackdemo.room

import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.java.jetpackdemo.R
import kotlinx.android.synthetic.main.item_room_recycle.view.*

class RoomAdapter :
    RecyclerView.Adapter<RoomAdapter.RoomViewHolder>() {
    var listData = mutableListOf<UserBean>()
        set(value) {
            listData.clear()
            listData.addAll(value)
        }

    override fun onBindViewHolder(holder: RoomViewHolder, position: Int) {
        val userBean = listData[position]
        holder.tvId.text = userBean.id.toString()
        holder.tvName.text = userBean.name
        holder.tvSex.text = userBean.sex
        holder.tvAge.text = userBean.age.toString()
        holder.tvEngName.text = userBean.engName
        holder.itemView.setOnClickListener {
            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse("http://www.baidu.com")
            holder.itemView.context.startActivity(intent)
        }
    }

    override fun getItemCount(): Int {
        return listData.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RoomViewHolder {
        val itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.item_room_recycle, parent, false)
        return RoomViewHolder(itemView)
    }


    class RoomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvId: TextView = itemView.tv_id
        var tvName: TextView = itemView.tv_name
        var tvSex: TextView = itemView.tv_sex
        var tvAge: TextView = itemView.tv_age
        var tvEngName: TextView = itemView.tv_engName
    }
}